package com.interview.elasticjob.sharding;

import com.dangdang.ddframe.job.lite.api.strategy.JobInstance;
import com.dangdang.ddframe.job.lite.api.strategy.JobShardingStrategy;

import java.util.*;

/**
 * @Function:  轮询
 * @Author: aqie
 * @Date: 2019/8/6 14:21
 */
public class MyShardingStrategy implements JobShardingStrategy {
    /**
     *
     * @param jobInstances          所有有效服务
     * @param jobName               job 名称
     * @param shardingTotalCount    总的分片数
     * @return 服务实例 实例获取的分片项
     */
    @Override
    public Map<JobInstance, List<Integer>> sharding(List<JobInstance> jobInstances,
                                                    String jobName, int shardingTotalCount) {
        Map<JobInstance, List<Integer>> rtnMap = new HashMap<>();
        ArrayDeque<Integer> queue = new ArrayDeque<>(shardingTotalCount);
        for (int i=0;i<shardingTotalCount;i++){
            queue.add(i);
        }

        while (queue.size()>0){
            for (JobInstance jobInstance : jobInstances){
                if (queue.size()>0){
                    Integer shardingItem = queue.pop();
                    List<Integer> integers = rtnMap.get(jobInstance);
                    if (integers!=null&&integers.size()>0){
                        integers.add(shardingItem);
                    }else {
                        List<Integer> list = new ArrayList<>();
                        list.add(shardingItem);
                        rtnMap.put(jobInstance,list);
                    }
                }
            }
        }

        return rtnMap;
    }
}
